﻿<splx:SplxUserControl x:Class="SuplexApp.SecurityPrincipalCtrl"
                   Name="securityPrincipalEditor"
                   KeyDown="spEditor_KeyDown"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:local="clr-namespace:SuplexApp"
                   xmlns:cc="clr-namespace:SuplexApp.Controls"
                   xmlns:splxApi="clr-namespace:Suplex.Forms.ObjectModel.Api;assembly=Suplex.Core"
                   xmlns:splx="clr-namespace:Suplex.Wpf;assembly=Suplex.Wpf"
                   >
    
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MaxWidth="700" MinWidth="400"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <StackPanel Orientation="Horizontal">
                <TextBlock Margin="5,5,0,5" FontSize="14" FontWeight="Bold" VerticalAlignment="Center" Foreground="DarkBlue"
                           TextBlock.Text="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=User:, ValueIfFalse=Group:, FormatString='\{0\} ' }"/>
                <TextBlock Margin="0,5,0,5" FontSize="14" FontWeight="Bold" VerticalAlignment="Center"
                           TextBlock.Text="{Binding Path=Name}"/>
                <TextBlock Margin="0,5,0,5" FontSize="18" FontWeight="Bold" VerticalAlignment="Top" Foreground="Red"
                           TextBlock.Text="*"
                           Visibility="{local:FormattedBinding Path=IsDirty, ValueIfTrue=Visible, ValueIfFalse=Hidden}"/>
            </StackPanel>
            <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Grid.Column="1">
                <Button Name="cmdOk" Content="Save"
                        VerticalAlignment="Center" MinHeight="20" Padding="5,0,5,0"
                        IsEnabled="{local:FormattedBinding Path=IsDirty}"
                        Click="cmdOk_Click"/>
                <Button Name="cmdCancel" Content="Discard"
                        VerticalAlignment="Center" MinHeight="20" Padding="5,0,5,0" Margin="5,0,2,0"
                        Click="cmdCancel_Click"/>
            </StackPanel>
        </Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <GroupBox Grid.Row="0" Margin="0,0,0,3" Header="Properties">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        
                        <Label Content="Name:" Style="{DynamicResource DialogLabel}"/>
                        <splx:SplxTextBox Grid.Column="1" Style="{DynamicResource DialogControl}" Name="txtName"
                                       Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/>

                        <Label Content="Description:" Grid.Row="1" VerticalAlignment="Top" Style="{DynamicResource DialogLabel}"/>
                        <splx:SplxTextBox Grid.Column="1" Grid.Row="1" Style="{DynamicResource DialogControl}" Height="52"
                                       AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
                                       Text="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged}"/>

                        <Label Content="Mask:" Grid.Row="2" Style="{DynamicResource DialogLabel}"
                               Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Collapsed, ValueIfFalse=Visible}"/>
                        <splx:SplxTextBox Grid.Column="1" Grid.Row="2" Style="{DynamicResource DialogControl}"
                                       Text="{Binding Path=MaskValue, UpdateSourceTrigger=PropertyChanged}"
                                       Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Collapsed, ValueIfFalse=Visible}"/>

                        <Label Content="Status:" Grid.Row="3" Style="{DynamicResource DialogLabel}"/>
                        <StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal" Margin="0,0,5,5">
                            <splx:SplxRadioButton Content="Enabled" VerticalAlignment="Center"
                                                  Margin="0,0,10,0" IsCheckedFixed="{Binding Path=IsEnabled}"/>

                            <splx:SplxRadioButton Content="Disabled" VerticalAlignment="Center"
                                                  IsCheckedFixed="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=False, ValueIfFalse=True, UpdateSourceTrigger=PropertyChanged}"/>
                        </StackPanel>

                        <Label Content="Type:" Grid.Row="4" Style="{DynamicResource DialogLabel}"
                               Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Collapsed, ValueIfFalse=Visible}"/>
                        <StackPanel Grid.Column="1" Grid.Row="4" Orientation="Horizontal" Margin="0,0,5,5"
                                    Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Collapsed, ValueIfFalse=Visible}">
                            <splx:SplxRadioButton Content="Local (Suplex)" VerticalAlignment="Center"
                                                  Margin="0,0,10,0" IsCheckedFixed="{Binding Path=IsLocal}"/>

                            <splx:SplxRadioButton Content="External (AD, LDAP, etc.)" VerticalAlignment="Center"
                                                  IsCheckedFixed="{local:FormattedBinding Path=IsLocal, ValueIfTrue=False, ValueIfFalse=True, UpdateSourceTrigger=PropertyChanged}"/>
                        </StackPanel>

                        <splx:ValidationSummary Grid.Row="5" Grid.Column="1" Name="vs" Margin="0,2,5,10"/>
                    </Grid>
                </GroupBox>

                <GroupBox Grid.Row="1" Margin="0,0,0,3" Header="Group Membership"
                          Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Visible, ValueIfFalse=Collapsed}">
                    <cc:DualListView x:Name="dlvMemberOf" MinHeight="200" Margin="5"
                                     AutoMoveItems="True" TrackAutoMovedItems="true"
                                     LeftHeader="Member Of" RightHeader="Not Member Of"
                                     MoveLeftClick="Membership_Changed_Left" MoveRightClick="Membership_Changed_Right">
                        <cc:DualListView.LeftListView>
                            <GridView AllowsColumnReorder="true">
                                <GridViewColumn Header="Name" Width="Auto" CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" />
                                <GridViewColumn Header="Description" Width="Auto" CellTemplate="{DynamicResource securityPrincipalDescriptionTemplate}"/>
                            </GridView>
                        </cc:DualListView.LeftListView>
                        <cc:DualListView.RightListView>
                            <GridView AllowsColumnReorder="true">
                                <GridViewColumn Header="Name" Width="Auto" CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" />
                                <!--<GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Description" Width="Auto"/>-->
                                <GridViewColumn Header="Description" Width="Auto" CellTemplate="{DynamicResource securityPrincipalDescriptionTemplate}"/>
                            </GridView>
                        </cc:DualListView.RightListView>
                    </cc:DualListView>
                </GroupBox>

                <Border Grid.Row="1" Margin="0,0,0,3" BorderThickness="0"
                        Visibility="{local:FormattedBinding Path=IsUserObject, ValueIfTrue=Collapsed, ValueIfFalse=Visible}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <Expander Header="Group Membership" Style="{DynamicResource GroupBoxStyle}" IsExpanded="True"
                                  Visibility="{local:FormattedBinding Path=IsLocal, ValueIfTrue=Visible, ValueIfFalse=Collapsed, UpdateSourceTrigger=PropertyChanged}">
                            <cc:DualListView x:Name="dlvMembers" MinHeight="200" MaxHeight="400" Margin="5"
                                     AutoMoveItems="True" TrackAutoMovedItems="true"
                                     LeftHeader="Members" RightHeader="Non Members"
                                     MoveLeftClick="Members_Changed_Left" MoveRightClick="Members_Changed_Right">
                                <cc:DualListView.LeftListView>
                                    <GridView AllowsColumnReorder="true">
                                        <GridViewColumn Header="Name" Width="Auto" CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" />
                                        <GridViewColumn Header="Description" Width="Auto" CellTemplate="{DynamicResource securityPrincipalDescriptionTemplate}"/>
                                    </GridView>
                                </cc:DualListView.LeftListView>
                                <cc:DualListView.RightListView>
                                    <GridView AllowsColumnReorder="true">
                                        <GridViewColumn Header="Name" Width="Auto" CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" />
                                        <GridViewColumn Header="Description" Width="Auto" CellTemplate="{DynamicResource securityPrincipalDescriptionTemplate}"/>
                                    </GridView>
                                </cc:DualListView.RightListView>
                            </cc:DualListView>
                        </Expander>

                        <Expander Header="Group Hierarchy" Grid.Row="1" Style="{DynamicResource GroupBoxStyle}" IsExpanded="True">
                            <splx:SplxTreeListView x:Name="tvwGroupHier" Grid.Row="1" BorderThickness="0" ItemsSource="{Binding}">
                                <splx:SplxTreeListView.Resources>
                                    <HierarchicalDataTemplate DataType="{x:Type splxApi:Group}" ItemsSource="{Binding Path=Groups}">
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </HierarchicalDataTemplate>
                                </splx:SplxTreeListView.Resources>
                                <!--<TreeView.ItemContainerStyle>
                                    <Style TargetType="{x:Type TreeViewItem}">
                                        <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}" />
                                    </Style>
                                </TreeView.ItemContainerStyle>-->
                                <splx:SplxTreeListView.Columns>
                                    <GridViewColumn CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" Header="Name" Width="200"/>
                                    <!--this doesn't work for some reason, and I have no idea why.  throws an idex error.-->
                                    <!--<GridViewColumn Header="Description" Width="Auto" CellTemplate="{DynamicResource securityPrincipalDescriptionTemplate}"/>-->
                                    <GridViewColumn Header="Description" Width="275">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate DataType="{x:Type splxApi:SecurityPrincipalBase}">
                                                <TextBlock Text="{Binding Path=Description}" Foreground="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=Black, ValueIfFalse=Gray}" VerticalAlignment="Center"/>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </splx:SplxTreeListView.Columns>
                            </splx:SplxTreeListView>
                        </Expander>
                    </Grid>
                </Border>

            </Grid>
        </ScrollViewer>
    </Grid>
</splx:SplxUserControl>